package org.flk;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Map;

public class mySink extends RichSinkFunction<Map<String, Object>> {
    private Connection connection=null;
    PreparedStatement ps=null;
    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName = sdc";
        connection = DriverManager.getConnection(url,"sa","sa123456");
        ps= connection.prepareStatement("insert into denyInfo (msg,save_time) values (?,?)");
    }


    @Override
    public void invoke(Map<String, Object> value, Context context) throws Exception {
         super.invoke(value, context);
         if(value.containsKey("msg")) {
             System.out.println("输出结果:" + value.get("msg").toString());
             ps.setString(1, value.get("msg").toString());
             ps.setString(2, value.get("saveTime").toString());
             ps.execute();
         }
    }

    @Override
    public void close() throws Exception {
        ps.close();
        connection.close();
        super.close();

    }
}
